var Module = function(){

	var handleValidation1 = function() {
        // for more info visit the official plugin documentation: 
            // http://docs.jquery.com/Plugins/Validation

            var form1 = $('#add_module_form');
            var error1 = $('.alert-danger', form1);

            form1.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    name: {
                        minlength: 2,
                        required: true
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit              
                    //success1.hide();
                    error1.show();
                    App.scrollTo(error1, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element).closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change done by hightlight
                    $(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
                },

                success: function (label) {
                    label.closest('.form-group').removeClass('has-error'); // set success class to the control group
                },

                submitHandler: function (form) {
                    function showResponse (responseText, statusText) { 
                        if(responseText.code === 0){
                            toastr.success("添加成功!");
                        } else {
                            toastr.error(responseText.msg);
                        }
                        $('#add_module_modal').modal('hide');
                    } 

                    var options = {
                        type:"post",
                        url:"user/module_add",
                        dataType:'json',
                        success:showResponse, 
                    }

                    form1.ajaxSubmit(options);
                    // $(form).submit(function() { 
                    //     form1.ajaxSubmit(options);
                    //     return false;   
                    // });                                        
                }
            });

    }

    var handleValidation2 = function() {
        // for more info visit the official plugin documentation: 
            // http://docs.jquery.com/Plugins/Validation

            var form1 = $('#add_action_form');
            var error1 = $('.alert-danger', form1);

            form1.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    name: {
                        minlength: 2,
                        required: true
                    },
                    url: {
                        required: true                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit              
                    //success1.hide();
                    error1.show();
                    App.scrollTo(error1, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element).closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change done by hightlight
                    $(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
                },

                success: function (label) {
                    label.closest('.form-group').removeClass('has-error'); // set success class to the control group
                },

                submitHandler: function (form) {
                        function showResponse (responseText, statusText) { 
                            if(responseText.code === 0){
                                toastr.success("添加成功!");
                            } else {
                                toastr.error(responseText.msg);
                            }
                            $('#add_action_modal').modal('hide');
                        } 

                        var options = {
                            type:"post",
                            url:"user/action_add",
                            dataType:'json',
                            success:       showResponse, 
                        }

                        form1.ajaxSubmit(options);                               
                }
            });

    }

	return {

		    init: function () {

		    	handleValidation1();

                handleValidation2();

                $('.module_name,.module_url,.module_icon').editable({
                    url: 'user/module_update',
                    ajaxOptions: {
                        dataType: 'json'
                    },
                    success:function(response,newValue){
                        console.log(response);
                        if(response.code==1) 
                        {return response.msg;} 
                    }
                });

                $('.action_name,.action_url,.action_icon').editable({
                    url: 'user/action_update',
                    ajaxOptions: {
                        dataType: 'json'
                    },
                    success:function(response,newValue){
                        console.log(response);
                        if(response.code==1) 
                        {return response.msg;} 
                    }
                });

                //添加动作
                $(document).on('click','.add_action_btn',function(){
                    //更改input值
                    var id = $(this).attr('data-pk');
                    var title = $(this).attr('data-title');
                    $('#module_hidden_input').val(id);
                    $('#module_name_input').val(title)
                });
		        
                //添加模块  
                $(document).on('click','.add_module_btn',function(){
                     var id = $(this).attr('data-pk');
                     if(id) {
                        $('#module_father').val(id);
                     }
                });
                    
		    	//删除模块
				$(document).on('click','.module_del_btn',function(){
					var id = $(this).attr('data-pk');
                    var tr = $(this).parents('tr');   
					if(id)
                    {
                        bootbox.confirm("确认删除模块？", function(result) {
                        if(result)
                        {
                            $.ajax({
                                url: 'user/module_delete',
                                type: 'POST',
                                dataType: 'json',
                                data: {id:id},
                            })
                            .done(function(response) {
                                if(response.code === 0) {
                                    toastr.success(response.msg);
                                    tr.remove();
                                } else {
                                    toastr.error(response.msg);
                                }
                            })
                            .fail(function() {
                                console.log("error");
                            })
                            .always(function() {
                                console.log("complete");
                            });
                            
                        }
                    });  
                    }
                   
				});

                //删除动作
                $(document).on('click','.action_del_btn',function(){
                    var id = $(this).attr('data-pk');
                    var tr = $(this).parents('tr');   
                    if(id)
                    {
                        bootbox.confirm("确认删除动作？", function(result) {
                        if(result)
                        {
                            $.ajax({
                                url: 'user/action_delete',
                                type: 'POST',
                                dataType: 'json',
                                data: {id:id},
                            })
                            .done(function(response) {
                                if(response.code === 0) {
                                    toastr.success(response.msg);
                                    tr.remove();
                                } else {
                                    toastr.error(response.msg);
                                }
                            })
                            .fail(function() {
                                console.log("error");
                            })
                            .always(function() {
                                console.log("complete");
                            });
                            
                        }
                    });  
                    }
                   
                });

		   	}
   		};

}();   